﻿<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>检测输入框的统一方法</title>
    <script type="text/javascript" language="JavaScript">
        function chk(){
            let b = document.getElementsByTagName("input");
            for(let i = 0;i< b.length;i++){
                if(b[i].name.substr(0,4) ==="item"){
                    if(b[i].value ===""){
                        alert("请输入第"+(i+1)+"项的值");
                        b[i].focus();
                    }
                    return false;
                }
            }
            return true;
        }
        
    </script>
</head>
<body>
<form name="form1" action="http://www.google.com" onSubmit="return chk()">
    1.<input name="item1" type="text" value="" >
    2.<input name="item2" type="text" value=""><br />
    3.<input name="item3" type="text" value="">
    4.<input name="item4" type="text" value=""><br />
    <input name="btn" type="submit" value="提交">
</form>
</body>
</html>